from datetime import datetime
from enum import Enum

from sqlalchemy import Column, DateTime, Integer, String, Boolean, func, ForeignKey, Table
from sqlalchemy.orm import relationship, backref

from ihome.mysql import Base


class User(Base):
    __tablename__ = "tb_user"
    id = Column(Integer, primary_key=True)
    username = Column(String(150), nullable=False, unique=True, index=True)
    password = Column(String(128), nullable=False)  # 使用密码哈希存储
    first_name = Column(String(30), nullable=True, default='')
    last_name = Column(String(150), nullable=True, default='')
    email = Column(String(254), nullable=True, unique=True, default='')
    is_active = Column(Boolean, default=True, nullable=False)
    is_staff = Column(Boolean, default=False, nullable=False)
    is_superuser = Column(Boolean, default=False, nullable=False)
    date_joined = Column(DateTime, default=datetime.utcnow, nullable=False)
    mobile = Column(String(11), unique=True, nullable=False)
    avatar = Column(String(50), nullable=True, default='')
    real_name = Column(String(32), nullable=True, default='')
    id_card = Column(String(20), nullable=True, default='')
    houses = relationship("House", back_populates="user")



class Area(Base):
    __tablename__ = "tb_area"
    """创建城区"""
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(20), nullable=False, comment="区域名字")
    create_time = Column(DateTime, default=func.now())
    update_time = Column(DateTime, default=func.now())


class Facility(Base):
    __tablename__ = "tb_facility"
    """设施信息"""
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(20), nullable=False, comment="设施名字")
    create_time = Column(DateTime, default=func.now())
    update_time = Column(DateTime, default=func.now())


house_facility = Table(
    'tb_house_facility',
    Base.metadata,
    Column('house_id', Integer, ForeignKey("tb_house.id")),
    Column('facility_id', Integer, ForeignKey('tb_facility.id'))
)


class House(Base):
    __tablename__ = "tb_house"
    """房屋信息"""
    id = Column(Integer, primary_key=True, index=True)
    create_time = Column(DateTime, default=func.now())
    update_time = Column(DateTime, default=func.now())
    user_id = Column(Integer, ForeignKey("tb_user.id"), doc="用户id")
    area_id = Column(Integer, ForeignKey("tb_area.id"), doc="归属地的区域编号")
    title = Column(String(50), nullable=False, comment="标题")
    price = Column(Integer, default=0)  # 单价，单价：分
    address = Column(String(100), default="")  # 地址
    room_count = Column(Integer, default=1)  # 房间数目
    acreage = Column(Integer, default=0)  # 房屋面积
    unit = Column(String(100), default="")  # 房屋单元
    capacity = Column(Integer, default=1)  # 房屋容纳的人数
    beds = Column(String(50), default="")  # 房屋床铺的配置
    deposit = Column(Integer, default=0)  # 房屋押金
    min_days = Column(Integer, default=1)  # 最少入住天数
    max_days = Column(Integer, default=0)  # 最多入住天数  0表示不限制
    order_count = Column(Integer, default=0)  # 预定完成的该房屋订单数
    index_image_url = Column(String(50), default="")  # 房屋图片路径
    facility = relationship('Facility', secondary=house_facility)
    user = relationship("User", back_populates="houses")  # 添加这一行，建立反向关系
    area = relationship("Area", backref=backref("houses", uselist=True))
    urls = relationship("HouseImage", back_populates="house")
    index_house =  Column(Integer,default=0)


class HouseImage(Base):
    __tablename__ = "tb_house_image"
    id = Column(Integer, primary_key=True, index=True)
    house_id = Column(Integer, ForeignKey("tb_house.id"), doc="房屋id")
    url = Column(String(50), nullable=False, comment="图片路径")
    create_time = Column(DateTime, default=func.now())
    update_time = Column(DateTime, default=func.now())
    house = relationship("House", back_populates="urls")


# 创建一个名为OrderStatusEnum的枚举类，用于表示status字段的选择限制

class OrderStatusEnum(Enum):
    WAIT_ACCEPT = 0  # 待接单
    WAIT_PAYMENT = 1  # 待支付
    PAID = 2  # 已支付
    WAIT_COMMENT = 3  # 待评价
    COMPLETE = 4  # 已完成
    CANCELED = 5  # 已取消
    REJECTED = 6  # 已拒单

class Order(Base):
    __tablename__ = "tb_order"
    """创建订单表"""
    id = Column(Integer, primary_key=True, index=True)
    create_time = Column(DateTime, default=func.now())
    update_time = Column(DateTime, default=func.now())
    user_id = Column(Integer, ForeignKey("tb_user.id"), doc="用户id")
    house_id = Column(Integer, ForeignKey("tb_house.id"), doc="房屋id")
    begin_date = Column(DateTime, comment="起始时间")
    end_date = Column(DateTime, comment="结束时间")
    days = Column(Integer, comment="预定总天数")
    house_price = Column(Integer, comment="房屋单价")
    amount = Column(Integer, comment="订单总金额")
    # 在Order模型中，将status字段的类型修改为Integer，并添加了一个status_enum的属性，用于获取对应的枚举值
    status = Column(Integer, default=0, index=True, comment="订单状态")
    comment = Column(String(80), comment="订单的评论信息或拒单理由")
    house = relationship("House", backref=backref("orders", cascade="all, delete-orphan"))
    user = relationship("User", backref=backref("orders", cascade="all, delete-orphan"))
    # 通过order.status_enum来获取status字段的枚举值

    @property
    def status_enum(self):
        return OrderStatusEnum(self.status)
